package com.byq.ai.agentlab.service;

import com.byq.ai.agentlab.entity.Agent;
import com.byq.ai.agentlab.repository.AgentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class AgentService {
    private final AgentRepository agentRepository;

    @Autowired
    public AgentService(AgentRepository agentRepository) {
        this.agentRepository = agentRepository;
    }

    public List<Agent> getAllAgents() {
        return agentRepository.findAll();
    }

    public Agent getAgentById(UUID id) {
        return agentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Agent not found with id: " + id));
    }

    @Transactional
    public Agent createAgent(Agent agent) {
        return agentRepository.save(agent);
    }

    @Transactional
    public Agent updateAgent(UUID id, Agent agentDetails) {
        Agent agent = getAgentById(id);
        agent.setName(agentDetails.getName());
        agent.setDescription(agentDetails.getDescription());
        agent.setStatus(agentDetails.getStatus());
        return agentRepository.save(agent);
    }

    @Transactional
    public void deleteAgent(UUID id) {
        Agent agent = getAgentById(id);
        agentRepository.delete(agent);
    }
} 